﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Rect2D
/// </summary>
public class Rect2D
{
	    /*			left < right
			    bottom < top
			    ^				top
			    |			-----------------		
			    |			|				|
			    |	left	|				|	right
			    |			|				|
			    |			-----------------
			    |				bottom
			    -------------------------------------------------->
    */

    public double left;
	public double top;
	public double right;
	public double bottom; 

    public Rect2D()
    {
	    left	= Double.MaxValue;
	    top		= Double.MaxValue;
	    right	= -Double.MaxValue;
	    bottom	= -Double.MaxValue;
    }
    	
    public Rect2D(double l, double t, double r, double b)
    {
	    left = l;
	    top = t;
	    right = r;
	    bottom = b;
    }

    public Rect2D(Rect2D srcRect)
    {
	    CopyRect(srcRect);
    }

    public double Width()
    {
	    return right - left;
    }

    public double Height()
    {
	    return top - bottom;
    }

    public Point2D TopLeft()
    {
	    return new Point2D(left, top);
    }

    public Point2D BottomRight()
    {
	    return new Point2D(right, bottom);
    }

    public Point2D CenterPoint()
    {
	    return new Point2D((left+right)/2.0, (top+bottom)/2.0);
    }

    public void Normalize()
    {
	    if (right < left)
	    {
		    double temp = left;
		    left = right;
		    right = temp;
	    }

	    if (top < bottom)
	    {
		    double temp = top;
		    top = bottom;
		    bottom = temp;
	    }
    }

    public void ExpandTo(double dX, double dY)
    {
	    left	=	Math.Min(left, dX);
	    top		=	Math.Max(top, dY);
	    right	=	Math.Max(right, dX);
	    bottom	=	Math.Min(bottom, dY);
    }

    public void ExpandTo(Point2D pPoint)
    {
	    left	=	Math.Min(left, pPoint.x);
	    top		=	Math.Max(top, pPoint.y);
	    right	=	Math.Max(right, pPoint.x);
	    bottom	=	Math.Min(bottom, pPoint.y);
    }

    public void ExpandTo(Rect2D pRect)
    {
	    left	=	Math.Min(left, pRect.left);
	    top		=	Math.Max(top, pRect.top);
	    right	=	Math.Max(right, pRect.right);
	    bottom	=	Math.Min(bottom, pRect.bottom);
    }

    public bool IsIntersect(Rect2D boundingRect)
    {
	    return (right <  boundingRect.left ||
			    bottom > boundingRect.top ||
			    left > boundingRect.right ||
			    top < boundingRect.bottom);
    }

    public bool IsRectNull()
    {
	    return (Math.Abs(left)  < Double.Epsilon && 
			    Math.Abs(top)   < Double.Epsilon && 
			    Math.Abs(right) < Double.Epsilon && 
			    Math.Abs(bottom)< Double.Epsilon);
    }

    public bool IsRectEmpty()
    {
	    return ((right - left) < Double.Epsilon || 
		    (top - bottom) < Double.Epsilon);
    }

    public bool PtInRect(Point2D point)
    {
	    return !((point.x < left) || (right < point.x) ||
			    (point.y < bottom) || (top < point.y));
    }

    public void SetRect(double xmin, double ymin, double xmax, double ymax)
    {
	    left = xmin;
	    bottom = ymin;
	    right = xmax;
	    top  = ymax;
    }

    /*public void GetRect(double& xmin, double& ymin, double& xmax, double& ymax)
    {
	    xmin = left;
	    ymin = bottom;
	    xmax = right;
	    ymax = top;
    }*/

    public void SetRect(Point2D bottomLeft, Point2D topRight)
    {
	    left	= bottomLeft.x;
	    bottom  = bottomLeft.y;
	    right	= topRight.x;
	    top		= topRight.y;
    }

    /*public void SetRect(Point2D pBottomLeft, Point2D pTopRight)
    {
	    left	= pBottomLeft.x;
	    bottom  = pBottomLeft.y;
	    right	= pTopRight.x;
	    top		= pTopRight.y;
    }*/

    public void SetRectEmpty()
    {
	    left	= Double.MaxValue;
        top = Double.MaxValue;
        right = -Double.MaxValue;
        bottom = -Double.MaxValue;
    }
	
    public void CopyRect(Rect2D srcRect)
    {
	    left = srcRect.left;
	    top = srcRect.top;
	    right = srcRect.right;
	    bottom = srcRect.bottom; 
    }

    public void InflateRect(double x, double y)
    {
	    left -= x;
	    top += y;
	    right += x;
	    bottom -= y;
    }

    public void DeflateRect(double x, double y)
    {
	    left += x;
	    top -= y;
	    right -= x;
	    bottom += y;
    }

    public void OffsetRect(double x, double y)
    {
	    left += x;
	    top += y;
	    right += x;
	    bottom += y;
    }
}
